package com.soft.mainsite.module.web.cms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.soft.mainsite.common.constant.CmsConst;
import com.soft.mainsite.common.domain.ErrorCode;
import com.soft.mainsite.common.utils.JsonUtil;
import com.soft.mainsite.module.staticstrategy.staticservice.StaticHtmlService;
import com.soft.mainsite.module.web.cms.service.SilderService;
import com.soft.mainsite.module.web.cms.service.SiteService;
import com.soft.mainsite.module.web.system.vo.UserVo;
import com.soft.mainsite.mybatis.mapper.TCmsAdSilderMapper;
import com.soft.mainsite.mybatis.model.TCmsAdSilder;
import com.soft.mainsite.mybatis.model.TCmsSite;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:首页幻灯service实现
 *
 * @author Jin
 * @create 2017-06-12
 **/
@Slf4j
@Service
public class SilderServiceImpl implements SilderService {

    @Autowired
    private TCmsAdSilderMapper silderMapper;
    @Autowired
    private SiteService siteService;
    @Autowired
    private StaticHtmlService staticHtmlService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String save(TCmsAdSilder pojo, UserVo userVo) {
        if (StringUtils.isBlank(pojo.getImg())) {
            log.error("首页幻灯图片为空！！");
            return JsonUtil.toError(ErrorCode.E_100401.getDesc());
        }
        pojo.setSiteId(userVo.getSiteId());
        pojo.setUserId(userVo.getUserId());
        // 默认不是主站创建的
        pojo.setPushId(0);
        int insertSelective = silderMapper.insertSelective(pojo);
        // 如果是主站  批量创建站点首页幻灯
        if (insertSelective > 0 && CmsConst.MAIN_SITE_ID.equals(userVo.getSiteId())) {
            this.pushMoreSilder(pojo);
        }
        // 首页静态化
        log.info("添加首页幻灯，子站单个首页静态化-----{}", pojo.getSiteId());
        try {
            staticHtmlService.index(pojo.getSiteId(), CmsConst.TPL_TYPE_MOBILE);
        } catch (Exception e) {
            log.error("添加首页幻灯，子站单个首页静态化失败", e);
        }
        return JsonUtil.toSuccess("操作成功", "ad-silder-tab", false);
    }

    @Override
    public String update(TCmsAdSilder pojo, UserVo userVo) {
        pojo.setSiteId(userVo.getSiteId());
        // 修改过后变为自己私有
        pojo.setPushId(0);
        int updateByPrimaryKeySelective = silderMapper.updateByPrimaryKeySelective(pojo);
        if (updateByPrimaryKeySelective > 0) {
            // 首页静态化
            log.info("修改首页幻灯，单个首页静态化-----{}", pojo.getSiteId());
            try {
                staticHtmlService.index(pojo.getSiteId(), CmsConst.TPL_TYPE_MOBILE);
            } catch (Exception e) {
                log.error("修改首页幻灯，单个首页静态化失败", e);
            }
            return JsonUtil.toSuccess("操作成功", "ad-silder-tab", false);
        }
        return JsonUtil.toError("操作失败");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String delete(Integer[] ids, UserVo userVo) {
        int deleteMoreSilder = silderMapper.deleteMoreSilder(ids);
        // 如果是主站需要删除未修改的子站站点首页幻灯
        if (deleteMoreSilder > 0 && CmsConst.MAIN_SITE_ID.equals(userVo.getSiteId())) {
            for (Integer id : ids) {
                List<Integer> siteIds = silderMapper.selectSiteIdByPushId(id);
                if (!siteIds.isEmpty()) {
                    silderMapper.deleteByPushId(id);
                }
            }
        }
        // 首页静态化
        log.info("删除首页幻灯，首页静态化-----{}", userVo.getSiteId());
        try {
            staticHtmlService.index(userVo.getSiteId(), CmsConst.TPL_TYPE_MOBILE);
        } catch (Exception e) {
            log.error("删除首页幻灯，静态化失败-----{}", userVo.getSiteId());
        }
        return JsonUtil.toSuccess("删除成功", "ad-silder-tab", false);
    }

    /**
     * 主站批量推送给子站点首页幻灯
     */
    private void pushMoreSilder(TCmsAdSilder pojo) {
        List<TCmsSite> siteList = siteService.findAll();
        List<TCmsAdSilder> adSilderList = new ArrayList<>();
        for (TCmsSite site : siteList) {
            if (CmsConst.MAIN_SITE_ID.equals(site.getSiteId())) {
                continue;
            }
            TCmsAdSilder adSilder = new TCmsAdSilder();
            BeanUtils.copyProperties(pojo, adSilder);
            adSilder.setId(null);
            adSilder.setSiteId(site.getSiteId());
            adSilder.setPushId(pojo.getId());
            adSilderList.add(adSilder);
        }
        this.saveMoreSilder(adSilderList);
    }

    @Override
    public int saveMoreSilder(List<TCmsAdSilder> adSilderList) {
        return silderMapper.insertMoreSilder(adSilderList);
    }

    @Override
    public List<TCmsAdSilder> findInitSilder() {
        return silderMapper.selectInitSilder(CmsConst.MAIN_SITE_ID);
    }

    @Override
    public int deleteMoreBySiteId(List<Integer> siteIds) {
        return silderMapper.deleteMoreBySiteId(siteIds);
    }

    @Override
    public TCmsAdSilder findById(Integer id) {
        return silderMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<TCmsAdSilder> findList(TCmsAdSilder pojo) {
        return silderMapper.select(pojo);
    }

    @Override
    public List<TCmsAdSilder> findAll() {
        return silderMapper.selectAll();
    }

    @Override
    public PageInfo<TCmsAdSilder> page(Integer pageNumber, Integer pageSize, TCmsAdSilder pojo) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(findList(pojo));
    }

    @Override
    public PageInfo<TCmsAdSilder> page(Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(findAll());
    }

    @Override
    public String save(TCmsAdSilder pojo) {
        return null;
    }

    @Override
    public String update(TCmsAdSilder pojo) {
        return null;
    }

    @Override
    public String delete(Integer[] ids) {
        return null;
    }
}
